<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Playlet;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\AppletCoupon;
use App\Common\Response\SystemCode;
use App\Service\playlet\AppletCouponService;

#[AutoController]
class AppletCouponController  extends BaseController
{

    #[Inject]
    private AppletCouponService $appletCouponService;
    /**
     * 小程序优惠券列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->appletCouponService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 小程序优惠券创建
     */
    public function Create(RequestInterface $request){

        $result = $this->appletCouponService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 小程序优惠券删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $appletCouponInfo = AppletCoupon::query()->where('applet_coupon.id', $id)
                ->select(['applet_coupon.id','applet.app_id','coupon_meta_id','applet_coupon.applet_id'])
                ->leftJoin('applet','applet.id','=','applet_coupon.applet_id')
                ->first();
            if (empty($appletCouponInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序券不存在');
            }
            $result = $this->appletCouponService->deleteCoupon($appletCouponInfo);
            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
            }
        }
    }
    /**
     * 小程序优惠券更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletCouponInfo = AppletCoupon::query()
                ->select(['id','applet_id','name','receive_begin_time','receive_end_time','valid_duration','valid_type','receive_desc','send_desc','consume_desc','coupon_meta_id'])
                ->where('id',$id)->first();
            if(empty($appletCouponInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序优惠券不存在');
            }
            if($request->getMethod() == 'GET'){

                $appletCouponInfo->receive_time = [$appletCouponInfo->receive_begin_time,$appletCouponInfo->receive_end_time];

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$appletCouponInfo);

            }else{
                $result = $this->appletCouponService->createOrUpdate($request->post(),true,$appletCouponInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }

            }
        }
    }
    /**
     * 小程序优惠券修改库存
     */
    public function updateMetaStock(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletCouponInfo = AppletCoupon::query()->where('applet_coupon.id', $id)
                ->select(['applet_coupon.id','applet.app_id','coupon_meta_id','stock_number','applet_coupon.name','applet_coupon.applet_id'])
                ->leftJoin('applet','applet.id','=','applet_coupon.applet_id')
                ->first();
            if(empty($appletCouponInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序优惠券不存在');
            }
            if($request->getMethod() == 'GET'){
                $selectRes = $this->appletCouponService->selectCoupon($appletCouponInfo);
                if($selectRes['err_no'] == 0){
                    $appletCouponInfo->stock_number = $selectRes['data']['stock_number'];
                    $appletCouponInfo->save();
                }


                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$appletCouponInfo);
            }else{
                $result = $this->appletCouponService->updateMetaStock($appletCouponInfo,$request->post());

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }

            }
        }
    }
    /**
     * 小程序优惠券修改状态
     */
    public function updateMetaStatus(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletCouponInfo = AppletCoupon::query()->where('applet_coupon.id', $id)
                ->select(['applet_coupon.id','applet_coupon.id','applet.app_id','coupon_meta_id','stock_number','applet_coupon.applet_id'])
                ->leftJoin('applet','applet.id','=','applet_coupon.applet_id')
                ->first();
            if(empty($appletCouponInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序优惠券不存在');
            }
            $result = $this->appletCouponService->updateMetaStatus($appletCouponInfo,$request->post());

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

            }
        }
    }
}